package yong.birthdayreminder;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class BuddyMgtActivity extends Activity implements OnClickListener{

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		adaptOrUpdateList();
	}
	private ListView lv = null; 
    private BirthdayReminderDBHelper dbhelper = null;
    private SQLiteDatabase db = null;
    private String SQLSelect = "select * from birthday;";
    private Button addButton;
    private Button searchButton;
    private EditText name;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		deleteBtn.setOnClickListener(this);
        dbhelper = new BirthdayReminderDBHelper(getBaseContext());
        db = dbhelper.getReadableDatabase();
        
		this.setContentView(R.layout.buddymgt);
		lv = (ListView)findViewById(R.id.buddymgt_listview);
		addButton = (Button)findViewById(R.id.buddymgt_button2);
		searchButton = (Button)findViewById(R.id.buddymgt_button1);
		name = (EditText)findViewById(R.id.buddymgt_edittext1);
		addButton.setOnClickListener(this);
		searchButton.setOnClickListener(this);
		adaptOrUpdateList();
	}
	private void adaptOrUpdateList (){
		Cursor bCursor = db.rawQuery(SQLSelect, null);
        startManagingCursor(bCursor);
		ListAdapter adapter = new SimpleCursorAdapter( 
                this, // Context. 
                R.layout.buddylist,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). 
                bCursor,                                              // Pass in the cursor to bind to. 
                new String[] {"name", "hobby","birthday"},          // Array of cursor columns to bind to. 
                new int[] {R.id.buddylist_testview1, R.id.buddylist_testview2, R.id.buddylist_testview3});  // Parallel array of which template objects to bind to those columns. 
        lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnBuddyItemClickListner());
	}
	private class OnBuddyItemClickListner implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(BuddyMgtActivity.this, EditBuddyActivity.class);
			Cursor c = db.rawQuery("select * from birthday where _id=" + arg3,
					null);
			c.moveToFirst();
			int i_name = c.getColumnIndex("name");
			int i_birthday = c.getColumnIndex("birthday");
			int i_hobby = c.getColumnIndex("hobby");
			intent.putExtra("name", c.getString(i_name));
			intent.putExtra("birthday", c.getString(i_birthday));
			intent.putExtra("hobby", c.getString(i_hobby));
			System.out.println("name:" + c.getString(i_name));
			System.out.println("hobby:" + c.getString(i_hobby));
			intent.putExtra("_id", arg3);
			startActivity(intent);

			System.out.println("ID is:" + arg3);
		}
	
	}	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		
		if (id == R.id.buddymgt_button2) {
			Intent intent = new Intent();
			intent.setClass(BuddyMgtActivity.this, AddNewBuddyActivity.class);
			startActivity(intent);
		} else if (id == R.id.buddymgt_button1){
			//To do -- Search by name
			Cursor bCursor;
			if (name.getText().toString().replaceAll(" ", "") != "") { 
			       bCursor = db.rawQuery("select * from birthday where name like \'%" + name.getText() + "%\';", null);
			} else {
			       bCursor = db.rawQuery(SQLSelect, null);
			}	
			startManagingCursor(bCursor);
			ListAdapter adapter = new SimpleCursorAdapter( 
	                this, // Context. 
	                R.layout.buddylist,  // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). 
	                bCursor,                                              // Pass in the cursor to bind to. 
	                new String[] {"name", "hobby","birthday"},          // Array of cursor columns to bind to. 
	                new int[] {R.id.buddylist_testview1, R.id.buddylist_testview2, R.id.buddylist_testview3});  // Parallel array of which template objects to bind to those columns. 
	        lv.setAdapter(adapter);
			lv.setOnItemClickListener(new OnBuddyItemClickListner());
		}	
	}
}
